﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Windows.Media;
using CuaHangBanhNgotBeoBeo.Game.ViewModel;
using GalaSoft.MvvmLight.Command;

namespace CuaHangBanhNgotBeoBeo.Game.View {
    public partial class ItemView : UserControl {

        public static readonly DependencyProperty posX;
        public double PosX {
            set { SetValue(posX, value); }
            get { return (double)GetValue(posX); }
        }

        public static readonly DependencyProperty posY;
        public double PosY {
            set { SetValue(posY, value); }
            get { return (double)GetValue(posY); }
        }

        static ItemView() {
            PropertyMetadata metadata = new PropertyMetadata(new double(), null);

            posX = DependencyProperty.Register("PosX", typeof(double), typeof(ItemView), metadata);
            posY = DependencyProperty.Register("PosY", typeof(double), typeof(ItemView), metadata);
        }

        public ItemView() {
            InitializeComponent();

            this.Loaded += ItemView_Loaded;

            this.dragBehavior.DragBegun += dragBehavior_DragBegun;
            this.dragBehavior.DragFinished += dragBehavior_DragFinished;
            this.dragBehavior.Dragging += dragBehavior_Dragging;
            this.dragBehavior.Dragging += HoverEffect;
        }

        void dragBehavior_Dragging(object sender, System.Windows.Input.MouseEventArgs e) {
            this.PosX = (this.RenderTransform as CompositeTransform).TranslateX;
            this.PosY = (this.RenderTransform as CompositeTransform).TranslateY;
        }

        void HoverEffect(object sender, System.Windows.Input.MouseEventArgs e) {
            Dictionary<String, Double> dic = new Dictionary<string, double>() { 
                {"x", this.PosX + double.Parse(this.GetValue(Canvas.LeftProperty).ToString())},
                {"y", this.PosY + double.Parse(this.GetValue(Canvas.TopProperty).ToString())},
                {"width", this.Width},
                {"height", this.Height}
            };

            (this.DataContext as ItemViewModel).SetHoverCommand.Execute(dic);
            //App.MainViewModel.SetHover(dic); 
           
        }

        private void dragBehavior_DraggingDisabled(object sender, System.Windows.Input.MouseEventArgs e) {
            (this.RenderTransform as CompositeTransform).TranslateX = this.PosX;
            (this.RenderTransform as CompositeTransform).TranslateY = this.PosY;
        }

        void dragBehavior_DragFinished(object sender, System.Windows.Input.MouseEventArgs e) {
            this.Dispatcher.BeginInvoke((Action)(() => {
                VisualStateManager.GoToState(this, "RevealState", true);
            }));

            (this.DataContext as ItemViewModel).SetFilledCommand.Execute(null);

            //this.Dispatcher.BeginInvoke((Action)(() => {
            //    VisualStateManager.GoToState(this, "HiddenState", true);
            //}));
        }

        void dragBehavior_DragBegun(object sender, System.Windows.Input.MouseEventArgs e) {
            this.Dispatcher.BeginInvoke((Action)(() => {
                VisualStateManager.GoToState(this, "DefaultState", true);
            }));

            (this.DataContext as ItemViewModel).SetClearedCommand.Execute(null);

        }

        void ItemView_Loaded(object sender, RoutedEventArgs e) {
            this.Dispatcher.BeginInvoke((Action)(() => {
                VisualStateManager.GoToState(this, "DefaultState", true);
            }));
        }

        private bool _isRevealed = false;

        private void UserControl_Tap_1(object sender, System.Windows.Input.GestureEventArgs e) {
            if (!_isRevealed) {
                this.Dispatcher.BeginInvoke((Action)(() => {
                    VisualStateManager.GoToState(this, "RevealState", true);
                }));

                _isRevealed = true;
            }
            else {
                this.Dispatcher.BeginInvoke((Action)(() => {
                    VisualStateManager.GoToState(this, "DefaultState", true);
                }));
                _isRevealed = false;
            }
        }
    }
}
